package com.demo.servlet.utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.aspose.words.*;
import com.demo.servlet.entity.Staff;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** 
* @Description: 表格添加数据
* @Param:  
* @return:  
* @Author: lizexin 
* @Date: 2020/12/21 
*/
public class AsposeUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(AsposeUtils.class);
    public static final String DocumentBuilderAndSaveOut = "DocumentBuilderAndSaveOut";
    public static final String output = "./output/";
    public static final String school_template = "school_template.docx";
    public static final long datast = new Date().getTime();

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = AsposeUtils.class.getClassLoader().getResourceAsStream("com.aspose.words.lic.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }//jiance

    /**
     * 保存doc path保存目录 doc原文档
     * @param
     * @param
     * @throws Exception
     */
    public static void main(String[] agrs) throws Exception {
        long old = System.currentTimeMillis();
        String path = "school_template.docx";
        Document doc = new Document(path);
        DocumentBuilder builder = new DocumentBuilder(doc);
        /*调整颜色*/
        // 获取word中的第一个表格，index指定表格位置
        Table table = (Table) doc.getChild(NodeType.TABLE, 1, true);
        Cell cell = (Cell)table.getRows().get(0).getCells().get(0);//获取第一行的第一个单元格
        //更改颜色 更改表格单元格中文本的颜色
       /* for (Run run : (Iterable<Run>)cell.getChildNodes(NodeType.RUN, true)){run.getFont().setColor(Color.BLUE);}
        table.getStyle().getFont().getColor().getBlue();
        table.getStyle().getFont().setColor(Color.black);//字体颜色
        //table.getStyle().getDocument().setPageColor(Color.BLUE);//文档背景颜色
        table.getStyle().getFont().getShading().setBackgroundPatternColor(Color.green);*/



        List<Staff> staffList = new ArrayList<>();
        Staff Staff = new Staff();
        Staff.setName("何志鹏");
        Staff.setSex("男");
        Staff.setiDNum("421123199108146035");
        Staff.setPhoneNum("11");
        Staff.setPost("java");
        Staff.setDictName("测试1");

        Staff staff2 = new Staff();
        staff2.setName("何志鹏2");
        staff2.setSex("男2");
        staff2.setiDNum("421123199108146035+2");
        staff2.setPhoneNum("112");
        staff2.setPost("java2");
        staff2.setDictName("测试2");

        Staff staff3 = new Staff();
        staff3.setName("何志鹏3");
        staff3.setSex("男3");
        staff3.setiDNum("421123199108146035+3");
        staff3.setPhoneNum("113");
        staff3.setPost("java3");
        staff3.setDictName("测试3");

        Staff staff4 = new Staff();
        staff4.setName("何志鹏4");
        staff4.setSex("男4");
        staff4.setiDNum("421123199108146035+3");
        staff4.setPhoneNum("114");
        staff4.setPost("java4");
        staff4.setDictName("测试4");

        staffList.add(Staff);
        staffList.add(staff2);
        staffList.add(staff3);
        staffList.add(staff4);

        for (Staff staff1 : staffList) { // 替换变量
            Node deepClone = table.getLastRow().deepClone(true);
            Range range = table.getLastRow().getRange();


            range.replace("name", staff1.getName(), true, true);
            range.replace("sex", staff1.getSex(), true, true);
            range.replace("post", staff1.getiDNum(), true, true);
            range.replace("idNum", staff1.getPhoneNum(), true, true);
            range.replace("phoneNum", staff1.getPost(), true, true);
            range.replace("dicName", staff1.getDictName(), true, true);
            range.replace("7", staff1.getDictName(), true, true);

            table.getRows().add(deepClone);

        }
        table.getLastRow().remove();

        String docxName = DocumentBuilderAndSaveOut+datast+".docx";
        doc.save(output+docxName);
        long now = System.currentTimeMillis();
        System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时

    }
}